<template>
  <div class="content">
    <van-nav-bar
      @click-left="$router.push('/index')"
      left-arrow
      title="文章"
      fixed
      @click-right="$router.push('/search')"
    >
      <template #right> <van-icon name="search" size="18" /> </template
    ></van-nav-bar>
    <div class="poster_type">
      <ul>
        <li :class="{ active: type == 1 }" @click="setType(1)">
          共享文库
          <i></i>
        </li>
        <li :class="{ active: type == 2 }" @click="setType(2)">
          我的文章
          <i></i>
        </li>
      </ul>
    </div>

    <!-- 文章 -->
    <div class="main" v-if="articleArr.length !== 0">
      <div
        class="item"
        @click="$router.push(`/detail?id=${item.id}&from=atricle&type=${type}`)"
        v-for="(item, index) in articleArr"
        :key="index"
      >
        <div class="left">
          <img :src="$publicObj.urlImg(item.cover)" />
          <img
            src="../../assets/images/task/getMoney.png"
            alt=""
            class="money"
            v-show="item.type == 9"
          />
        </div>
        <div class="right">
          <span class="title">{{ item.title }}</span>
          <span class="p">{{ item.description }}</span>
          <div class="bottom">
            <span>转发:{{ item.share }}次</span>
            <span>阅读:{{ item.yuedu || 0 }}次</span>
            <span>上传时间:{{ item.gmtCreate.substring(0, 11) }}</span>
          </div>
        </div>
      </div>
    </div>

    <van-empty description="暂无数据" v-else />
  </div>
</template>

<script>
import extract from "../../plugins/extract.js";
import noVisit from "../../components/noVisit.vue";
export default {
  components: {
    noVisit,
  },
  data() {
    return {
      articleArr: [], //文章列表
      page: 1, //当前页数,
      totalPage: "",
      agentId: "",
      wxUserInfo: {}, //微信用户个人信息
      type: 1,
      tabelType: 1,
    };
  },

  async mounted() {
    if (!JSON.parse(sessionStorage.getItem("userWxData"))) {
      await extract.wxShare();
    }
    this.wxUserInfo = JSON.parse(sessionStorage.getItem("userWxData"));

    if (this.$route.query.account) {
      sessionStorage.setItem("agentId", this.$route.query.account);
    }
    this.agentId = sessionStorage.getItem("agentId");

    this.getArticle();
    // 获取个人信息
    this.$store.dispatch("updateUserInfo");
  },

  methods: {
    // 切换
    setType(n) {
      this.type = n;
      this.getArticle();
    },

    //获取文章列表
    async getArticle() {
      let obj = {
        page: 1,
        limit: 10000000,
        // type: 8,
        userId: this.agentId,
        openId: this.wxUserInfo.openid,
        name: this.wxUserInfo.nickname,
        image: this.wxUserInfo.headimgurl,
        sex: this.wxUserInfo.sex,
        city: this.wxUserInfo.city,
        province: this.wxUserInfo.province,
        tabelType: 1,
        sortType: null, //排序
        account: sessionStorage.getItem("agentId"),
      };
      if (this.type == 1) {
        obj.type = 10;
      }

      let res = await this.$api.getArticleArr(obj);

      if (res.code != 0) return;
      if (res.status == 0) {
        this.$router.push("/default");
        return;
      }
      this.articleArr = res.page.list;
      sessionStorage.setItem("agentIds", res.agentId);
      this.totalPage = res.page.totalPage;
    },
  },
};
</script>

<style scoped lang="less">
.poster_type {
  margin-top: 46px;
  background-color: #fff;
  ul {
    display: -webkit-flex;
    display: flex;
    justify-content: space-evenly;
    li {
      font-size: 16px;
      color: #666;
      display: -webkit-flex;
      display: flex;
      justify-content: center;
      position: relative;
      align-items: center;
      padding: 5px 0;
      i {
        width: 28px;
        height: 3px;

        border-radius: 3px;
        position: absolute;
        bottom: 0;
      }
    }
    .active {
      font-weight: 700;
      color: #000;
      i {
        background-color: #0264d1;
      }
    }
  }
}
.content {
  padding: 15px 15px 0;

  .main {
    height: calc(100vh - 150px);
    overflow-y: scroll;
    .item {
      display: flex;
      margin-top: 12px;
      .left {
        flex: 1;
        width: 90px;
        height: 90px;
        margin-right: 15px;
        position: relative;
        img {
          width: 90px;
          height: 90px;
          border-radius: 5px;
        }
        .money {
          width: 35px;
          height: 35px;
          position: absolute;
          right: 0;
          top: 0;
          border-radius: none;
        }
      }
      .right {
        flex: 3;
        span {
          font-size: 11px;
          color: #999999;
        }
        .title {
          display: block;
          font-size: 14px;
          color: #333333;
        }
        .p {
          margin-top: 3px;
          height: 30px;
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
        }
        .bottom {
          width: 246px;
          margin-top: 8px;
          display: flex;
          justify-content: space-between;
        }
      }
    }
  }
}
</style>
